<?php

namespace App\Http\Controllers\Api;

use App\Common\Libs\ArrayHelper;
use App\Consts\GlobalTopicConst;
use App\Exceptions\Error;
use App\Http\Controllers\BaseController;
use Illuminate\Http\Request;

class TopicController extends BaseController
{
    public $validate = [
        "getDetail" => [
            "rules" => [
                "id" => "required|string|max:24",
            ]
        ],
        "promptList" => [
            "rules" => [
                "topic_id" => "required|string|max:24",
            ]
        ],
    ];

    /**
     * @path /topic/get-detail
     * @category 话题
     * @title 详情
     * @method POST
     * @params [{"name":"id","type":"text","required":1,"example":"","desc":"话题ID"}]
     * @resBody {"success":true,"statusCode":200,"result":{"id":"话题ID","description":"话题描述","title":"话题标题","is_deleted":0,"created_at":"创建时间戳","top":[{"id":"24位文章笔记或划线笔记ID","type":"笔记类型，1文章笔记，2划线笔记，3随手记","user_id":"24位用户ID","article_id":"24位文章ID","is_deleted":0,"is_public":1,"created_at":"10位时间戳","start_index":"文章节选开始索引","end_index":"文章节选结束索引","content":"笔记内容","selected":"文章节选内容","is_like":0,"is_collect":1,"user":{"id":"24位用户ID","nickname":"用户昵称","avatar":"头像，如1.jpg"},"article":{"id":"24位文章ID","cover_url":"文章封面图，如1.jpg","title":"文章标题","description":"文章描述","is_deleted":0,"is_shield":0,"created_at":"文章创建时间戳"},"count":{"comment":"评论数","like":"点赞数","collect":"收藏数"}},{"id":"24位随手记ID","type":"笔记类型，1文章笔记，2划线笔记，3随手记","user_id":"24位用户ID","article_id":"","is_deleted":0,"is_public":1,"created_at":"10位时间戳","start_index":0,"end_index":0,"content":"笔记内容","selected":"","is_like":0,"is_collect":1,"user":{"id":"24位用户ID","nickname":"用户昵称","avatar":"头像，如1.jpg"},"article":{},"topic":[{"id":"话题ID","title":"话题标题"}],"count":{"comment":"评论数","like":"点赞数","collect":"收藏数"}}],"hot":[{"id":"24位文章笔记或划线笔记ID","type":"笔记类型，1文章笔记，2划线笔记，3随手记","user_id":"24位用户ID","article_id":"24位文章ID","is_deleted":0,"is_public":1,"created_at":"10位时间戳","start_index":"文章节选开始索引","end_index":"文章节选结束索引","content":"笔记内容","selected":"文章节选内容","is_like":0,"is_collect":1,"user":{"id":"24位用户ID","nickname":"用户昵称","avatar":"头像，如1.jpg"},"article":{"id":"24位文章ID","cover_url":"文章封面图，如1.jpg","title":"文章标题","description":"文章描述","is_deleted":0,"is_shield":0,"created_at":"文章创建时间戳"},"count":{"comment":"评论数","like":"点赞数","collect":"收藏数"}},{"id":"24位随手记ID","type":"笔记类型，1文章笔记，2划线笔记，3随手记","user_id":"24位用户ID","article_id":"","is_deleted":0,"is_public":1,"created_at":"10位时间戳","start_index":0,"end_index":0,"content":"笔记内容","selected":"","is_like":0,"is_collect":1,"user":{"id":"24位用户ID","nickname":"用户昵称","avatar":"头像，如1.jpg"},"image":["1.jpg"],"article":{},"topic":[{"id":"话题ID","title":"话题标题"}],"count":{"comment":"评论数","like":"点赞数","collect":"收藏数"}}]},"message":"OK"}
     */
    public function getDetail(Request $request)
    {
        $topicId = $request->post('id');

        $topic = service()->TopicService->getTopic($topicId);
        if (!$topic) {
            return $this->apiReturn(Error::handle(lang('Topic is disable')));
        }

        $moreInfo = service()->TopicService->getTopicMoreInfo($topic, $this->getCurrentUserId());

        return $this->apiReturn(ArrayHelper::merge($topic->commonFormat(), $moreInfo));
    }

    /**
     * @path /topic/prompt
     * @category 话题
     * @title 实时主体内容列表
     * @method POST
     * @params [{"name":"topic_id","type":"text","required":1,"example":"","desc":"话题ID"},{"name":"last_id","type":"text","required":0,"example":"","desc":"【翻页时必传】当前页最后一条记录的ID"}]
     * @resBody {"success":true,"statusCode":200,"result":{"id":"话题ID","description":"话题描述","title":"话题标题","is_deleted":0,"created_at":"创建时间戳","top":[{"id":"24位文章笔记或划线笔记ID","type":"笔记类型，1文章笔记，2划线笔记，3随手记","user_id":"24位用户ID","article_id":"24位文章ID","is_deleted":0,"is_public":1,"created_at":"10位时间戳","start_index":"文章节选开始索引","end_index":"文章节选结束索引","content":"笔记内容","selected":"文章节选内容","is_like":0,"is_collect":1,"user":{"id":"24位用户ID","nickname":"用户昵称","avatar":"头像，如1.jpg"},"article":{"id":"24位文章ID","cover_url":"文章封面图，如1.jpg","title":"文章标题","description":"文章描述","is_deleted":0,"is_shield":0,"created_at":"文章创建时间戳"},"count":{"comment":"评论数","like":"点赞数","collect":"收藏数"}},{"id":"24位随手记ID","type":"笔记类型，1文章笔记，2划线笔记，3随手记","user_id":"24位用户ID","article_id":"","is_deleted":0,"is_public":1,"created_at":"10位时间戳","start_index":0,"end_index":0,"content":"笔记内容","selected":"","is_like":0,"is_collect":1,"user":{"id":"24位用户ID","nickname":"用户昵称","avatar":"头像，如1.jpg"},"article":{},"topic":[{"id":"话题ID","title":"话题标题"}],"count":{"comment":"评论数","like":"点赞数","collect":"收藏数"}}],"hot":[{"id":"24位文章笔记或划线笔记ID","type":"笔记类型，1文章笔记，2划线笔记，3随手记","user_id":"24位用户ID","article_id":"24位文章ID","is_deleted":0,"is_public":1,"created_at":"10位时间戳","start_index":"文章节选开始索引","end_index":"文章节选结束索引","content":"笔记内容","selected":"文章节选内容","is_like":0,"is_collect":1,"user":{"id":"24位用户ID","nickname":"用户昵称","avatar":"头像，如1.jpg"},"article":{"id":"24位文章ID","cover_url":"文章封面图，如1.jpg","title":"文章标题","description":"文章描述","is_deleted":0,"is_shield":0,"created_at":"文章创建时间戳"},"count":{"comment":"评论数","like":"点赞数","collect":"收藏数"}},{"id":"24位随手记ID","type":"笔记类型，1文章笔记，2划线笔记，3随手记","user_id":"24位用户ID","article_id":"","is_deleted":0,"is_public":1,"created_at":"10位时间戳","start_index":0,"end_index":0,"content":"笔记内容","selected":"","is_like":0,"is_collect":1,"user":{"id":"24位用户ID","nickname":"用户昵称","avatar":"头像，如1.jpg"},"image":["1.jpg"],"article":{},"topic":[{"id":"话题ID","title":"话题标题"}],"count":{"comment":"评论数","like":"点赞数","collect":"收藏数"}}]},"message":"OK"}
     */
    public function promptList(Request $request)
    {
        $searchParams = $request->post();

        $list = service()->TopicService->getTopicTargets(ArrayHelper::merge($searchParams, [
            'current_user_id' => $this->getCurrentUserId(),
        ]));

        return $this->apiReturn($list);
    }

    /**
     * @path /topic/hot
     * @category 话题
     * @title 热门话题列表
     * @method POST
     * @resBody {"success":true,"statusCode":200,"result":{"have_more":false,"list":[{"id":"话题ID","description":"话题描述","title":"话题标题","is_deleted":0,"updated_at":"更新时间戳","count":{"hot":"热度"}}]},"message":"OK"}
     */
    public function hotList(Request $request)
    {
        $searchParams = $request->post();

        $list = service()->TopicService->getTopicList(ArrayHelper::merge($searchParams, [
            'current_user_id' => $this->getCurrentUserId(),
            'is_hot' => GlobalTopicConst::IS_HOT,
        ]));

        return $this->apiReturn($list);
    }
}
